package com.wsm.kotlintest.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.wsm.kotlintest.viewmodel.ui.UIController

/**
 * Created by 王家宝 on 2018/7/20.
 * TEL:18241150668
 * MAIL:639586819@qq.com
 */
@Suppress("UNCHECKED_CAST")
abstract class BaseFragment<BINDING : ViewDataBinding, VM : ViewModel>(vM: Class<out ViewModel>) : Fragment(), UIController {
    private var viewModelReified: Class<out ViewModel>? = vM
    lateinit var viewModel: VM
    var binding: BINDING? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        viewModel = ViewModelProviders.of(this).get(viewModelReified!!) as VM
        binding = DataBindingUtil.inflate(inflater, setLayout(), container, false)
        inject()
        binding?.setLifecycleOwner(this)
        return binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        onBindView(view)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        doSomething()
    }

    abstract fun onBindView(view: View)

}